<template>
    <div class="app-container">
      <!-- 搜索区域 -->
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        size="small"
      >
        <el-form-item label="选手" prop="playerName">
          <el-input
            v-model="queryParams.playerName"
            placeholder="请输入选手名称"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item label="文案" prop="content">
          <el-input
            v-model="queryParams.content"
            placeholder="请输入文案"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >查询</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
  
      <!-- 操作按钮区域 -->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            @click="handleTimeUpdate"
            >臂环显示</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="info"
            icon="el-icon-upload"
            size="mini"
            @click="handleUpload"
            >上传自定义值</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
  
      <!-- 表格区域 -->
      <el-table v-loading="loading" :data="playerList" style="width: 100%">
        <el-table-column label="选手姓名" prop="name" align="center" />
        <el-table-column label="学校" prop="school" align="center" />
        <el-table-column label="文案" prop="content" align="center" />
        <el-table-column label="图像" prop="image" align="center" />
        <el-table-column label="视频" prop="video" align="center" />
        <el-table-column label="创建时间" prop="createTime" align="center" />
        <el-table-column label="操作" align="center" width="150" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleEdit(scope.row)"
              >修改</el-button
            >
            <el-button
              type="text"
              size="mini"
              @click="handleEliminate(scope.row)"
              v-if="scope.row.status === 1"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
  
      <!-- 分页组件 -->
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <edit-player
        ref="editPlayer"
        :visible.sync="editDialogVisible"
        :title="editDialogTitle"
        @success="handleEditSuccess"
      />
    </div>
  </template>
  
  <script>
  import { listPlayer, eliminatePlayer } from "@/api/player";
  import EditPlayer from "./components/editPlayer.vue";
  import Pagination from "@/components/Pagination";
  
  export default {
    name: "Square",
    components: {
      EditPlayer,
      Pagination,
    },
    data() {
      return {
        // 遮罩层
        loading: false,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 选手列表数据
        playerList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          status: undefined,
        },
        editDialogVisible: false,
        editDialogTitle: "新增选手",
      };
    },
    created() {
      this.getList();
    },
    methods: {
      // 获取列表数据
      getList() {
        this.loading = true;
        listPlayer(this.queryParams)
          .then((res) => {
            console.log(res);
            this.playerList = res.rows;
            this.total = res.total;
          })
          .catch((error) => {
            console.error("获取列表失败:", error);
          })
          .finally(() => {
            this.loading = false;
          });
      },
  
      // 查询按钮操作
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
  
      // 重置按钮操作
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
        
      },
  
      // 新增按钮操作
      handleAdd() {
        this.editDialogTitle = "新增选手";
        this.editDialogVisible = true;
      },
  
      // 修改按钮操作
      handleEdit(row) {
        this.editDialogTitle = "修改选手";
        this.editDialogVisible = true;
        this.$nextTick(() => {
          if (this.$refs.editPlayer) {
            const formData = {
              id: row.id,
              name: row.name,
              status: row.status,
              deviceId: row.deviceId,
              phoneNum: row.phoneNum,
              wechatId: row.wechatId,
              fansCount: row.fansCount,
              expireValue: row.expireValue,
              customizeValue: row.customizeValue,
            };
            this.$refs.editPlayer.setFormData(formData);
          }
        });
      },
  
      // 淘汰选手按钮操作
      handleEliminate(row) {
        this.$modal
          .confirm("确认要淘汰该选手吗？")
          .then(() => {
            eliminatePlayer(row.id)
              .then((res) => {
                this.$modal.msgSuccess("淘汰成功");
                this.getList();
              })
              .catch((err) => {
                console.error("淘汰失败:", err);
              });
          })
          .catch(() => {});
      },
  
      // 编辑成功回调
      handleEditSuccess() {
        this.getList();
      },
  
      // 上传自定义值按钮操作
      handleUpload() {
        this.$modal.msgWarning("功能开发中...");
      },
  
      // 臂环显示按钮操作
      handleTimeUpdate() {
        this.$modal.msgWarning("功能开发中...");
      },
    },
  };
  </script>
  
  <style scoped>
  .app-container {
    padding: 20px;
  }
  .mb8 {
    margin-bottom: 8px;
  }
  </style>
  